package com.puzzlebazar.client.puzzleinfo;

import java.util.HashMap;

import com.puzzlebazar.client.main.control.MainController;
import com.puzzlebazar.client.main.control.NamedStateController;
import com.puzzlebazar.client.main.control.StateWithSubstates;
import com.puzzlebazar.client.main.view.MainView;
import com.puzzlebazar.client.user.UserModel;

public class PuzzleInfoState extends StateWithSubstates {
	
	public static final String STATE_TOKEN = "PuzzleInfo";
	private static final String PUZZLE_KEY = "PKey";
	private static final String TUTORIAL_KEY = "TKey";
	public static final String PUZZLE_TITLE = "Puzzle Information";
	public static final String TUTORIAL_TITLE = "Tutorial Information";

	private final PuzzleInfoModel puzzleInfoModel = new PuzzleInfoModel();
	private final PuzzleInfoController puzzleInfoController;
		
	public PuzzleInfoState( UserModel model, MainView view, MainController controller ) {
		super( model, view, controller );		
		
		puzzleInfoController = new PuzzleInfoController( puzzleInfoModel, tabbedView, controller, subStateTokenFactory );
	}

	@Override
	public String getStateToken() {
		return STATE_TOKEN;
	}

	/**
	 * Get the current parameters of the state provided a puzzleId
	 * 
	 * @param puzzleKey The puzzle key
	 * @param stateName The name of the state for the subcontroller (null for default state)
	 * @param isTutorial True if this puzzle key is for a tutorial, false otherwise
	 * @return The parameters of the state
	 */
	public static HashMap<String,String> getParams( String puzzleKey, String stateName, boolean isTutorial ) {
		HashMap<String,String> params = StateWithSubstates.getParams( stateName );
		if( isTutorial )
			params.put( TUTORIAL_KEY, puzzleKey );
		else
			params.put( PUZZLE_KEY, puzzleKey );
		return params;
	}

	@Override
	public HashMap<String,String> getParams() {
		HashMap<String,String> params = super.getParams();
		addCustomParams( params );
		return params;
	}

	/**
	 * Sets the parameters for the puzzle for which to get information
	 * 
	 * @param puzzleKey The puzzle for which to load information 
	 * @param stateName The state the subcontroller should be in (null for default state)
	 * @param isTutorial True if this puzzle key is for a tutorial, false otherwise
	 */
	public void setParams( String puzzleKey, String stateName, boolean isTutorial ) {
		if( isTutorial )
			puzzleInfoModel.setTutorialPuzzleKey( puzzleKey );
		else
			puzzleInfoModel.setPuzzleKey( puzzleKey );
		puzzleInfoModel.notifyObservers();
		super.setParams(stateName);
	}

	@Override
	public void setParams( HashMap<String,String> params ) throws Exception {
		try {
			String tutorialPuzzleKey = params.get( TUTORIAL_KEY );
			if( tutorialPuzzleKey != null )
				puzzleInfoModel.setTutorialPuzzleKey( tutorialPuzzleKey );
			else
				puzzleInfoModel.setPuzzleKey( params.get( PUZZLE_KEY ) );
			puzzleInfoModel.notifyObservers();
			super.setParams( params );
		}
		catch( Exception exception ) {
			setParams( null, null, false );
			exception.printStackTrace();
			throw new Exception( "Invalid parameters" );
		}
	}

	@Override
	protected String getMainPanelTitle() {
		if( puzzleInfoModel.isTutorial() )
			return TUTORIAL_TITLE;
		else
			return PUZZLE_TITLE;
	}

	@Override
	protected NamedStateController getSubController() {
		return puzzleInfoController;
	}

	@Override
	protected void addCustomParams(HashMap<String, String> params) {
		if( puzzleInfoModel.isTutorial() )
			params.put(TUTORIAL_KEY, puzzleInfoModel.getPuzzleKey() );		
		else
			params.put(PUZZLE_KEY, puzzleInfoModel.getPuzzleKey() );		
			
	}

	@Override
	public void release() {
		super.release();
		puzzleInfoModel.release();
		puzzleInfoController.release();
	}

}
